package renshi.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseDAO;
import renshi.model.Apply;
import renshi.model.Student;
import renshi.model.Tclass;
import renshi.utils.Calculatedate;

public class StudentDAO extends BaseDAO {
	HttpServletRequest request = ServletActionContext.getRequest();
	public StudentDAO(Connection con) {
		super(con);
	}
	
	//---------�޸�����
	public boolean updatepwd(String name, String newpwd){
		int flag=0;
		String sql="update Student set pwd='"+newpwd+"' where num='"+name+"'";
		try {
			pstmt=con.prepareStatement(sql);
			 flag=pstmt.executeUpdate();
			if(flag!=0){
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		
		return true;
	}
	
	public boolean delete(int id){
		return true;
	}
	//---------------ѧ���¼
	public boolean login(String name, String pwd){
		String sql = "select * from Student where num='"+name+"' and pwd='"+pwd+"'";
		
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			
			if(rs.next()){
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		
		return true;
	}
	
	public List<Student> queryall(){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from Student";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Student student=new Student();
				student.setName(rs.getString("name"));
				student.setCname(rs.getString("cname"));
				
				list.add(student);
			}
			return list;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean update(Student student){
		return true;
	}
	//----------��д��ٵ�
	public boolean apply(String loginname,String begintime,String endtime,String reason,String remark,int status){
		
		Calculatedate cal=new Calculatedate(begintime,endtime);
		cal.calculatedate();
		System.out.println("555555555555"+begintime);
    String sql = "insert into Apply (num,begintime,endtime,reason,remark,status,days) values ('"+loginname+"','"+begintime+"','"+endtime+"','"+reason+"','"+remark+"',1,'"+cal.getNumday()+"')";
		
		try {
			stmt = con.createStatement();
			int flag = stmt.executeUpdate(sql);
			
			if(flag>0){
				
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		
		return true;
	}
	//-----------��ѯѧ��İ༶ 
	public Student querystudent(String name){
		
    String sql = "select * from Student where num='"+name+"'";
    System.out.println("rs.nextǰ���studentDAO��"+name);
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next()){
				Student student=new Student();
				student.setCname(rs.getString("cname"));
				return student;
			}
			else
				return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	
		
	}
	//--------------��ѯѧ�����ʦ
	public Tclass queryteacher(String cname){
		
	    String sql = "select * from Class where cname='"+cname+"'";
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					Tclass tclass=new Tclass();
					tclass.setTname(rs.getString("tname"));
					return tclass;
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}
	//--------------------�ж�ѧ������״̬
	public Apply status(String name){
		
	    String sql = "select * from Apply where num='"+name+"'";
			
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
				
				if(rs.next()){
					Apply apply=new Apply();
					apply.setStatus(rs.getInt("status"));
					apply.setReason(rs.getString("reason"));
					apply.setBegintime(rs.getString("begintime"));
					apply.setEndtime(rs.getString("endtime"));
					apply.setDays(rs.getInt("days"));
					apply.setRemark(rs.getString("remark"));
					request.setAttribute("apply", apply);
					return apply;
					
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}
	//----------------�ж��Ƿ�����������
public Apply applyLeave(String name){
		
	    String sql = "select * from Apply where num='"+name+"' and (status=1 or status=2) order by aid DESC";
			
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
				
				if(rs.next()){
					Apply apply=new Apply();
					apply.setStatus(rs.getInt("status"));
					request.setAttribute("apply", apply);
					System.out.print("�ж��Ƿ��������е�״̬"+rs.getInt("status"));
					return apply;
					
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}
	//---------------------�ж�ѧ����ٵ���״̬
      
	public Apply applystatus(String loginname){
		
	    String sql = "select * from Apply where num='"+loginname+"' and status!=4 order by aid DESC";
			
	    try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			
			if(rs.next()){
				Apply applystatus=new Apply();
				applystatus.setLoginname(rs.getString("num"));
				applystatus.setReason(rs.getString("reason"));
				applystatus.setRemark(rs.getString("remark"));
				applystatus.setStatus(rs.getInt("status"));
				applystatus.setBegintime(rs.getString("begintime"));
				applystatus.setEndtime(rs.getString("endtime"));
				applystatus.setDays(rs.getInt("days"));
				request.setAttribute("applystatus", applystatus);
				return applystatus;
				
			}
			else
				return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		}
//--------------��ȡ��ʷ��ٵ���Ϣ
	public List<Apply> queryApplylist(String name){
		List<Apply> applylist = new ArrayList<Apply>();
		String sql = "select * from Apply where num='"+name+"' and status!=1 and status!=2";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Apply apply=new Apply();
				apply.setAid(rs.getInt("aid"));
				apply.setLoginname(rs.getString("num"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				apply.setDays(rs.getInt("days"));
            	applylist.add(apply);
			}
			return applylist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//------------------��ȡ��ٵ�����ϸ��Ϣ
public Apply applydetail(int numid){
		
	    String sql = "select * from Apply where aid='"+numid+"'";
			
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
				
				if(rs.next()){
					Apply apply=new Apply();
					apply.setAid(rs.getInt("aid"));
					apply.setLoginname(rs.getString("num"));
					apply.setBegintime(rs.getString("begintime"));
					apply.setEndtime(rs.getString("endtime"));
					apply.setDays(rs.getInt("days"));
					apply.setReason(rs.getString("reason"));
					apply.setRemark(rs.getString("remark"));
					apply.setStatus(rs.getInt("status"));
					request.setAttribute("apply", apply);
					System.out.println("dadgdgdhgdh"+rs.getInt("days"));

					return apply;
					
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}
//------------��ȡ��ʷ��������������������
public List<Apply> queryhistory(String name){
	List<Apply> applyhistory = new ArrayList<Apply>();
	String sql = "select * from Apply where num='"+name+"'";
	//������ݿ�
	try{
		stmt=con.createStatement();
		rs=stmt.executeQuery(sql);
		while(rs.next()){
			Apply apply=new Apply();
			apply.setAid(rs.getInt("aid"));
			apply.setReason(rs.getString("reason"));
			apply.setRemark(rs.getString("remark"));
			apply.setDays(rs.getInt("days"));
			applyhistory.add(apply);
		}
		return applyhistory;
	}
	catch(SQLException e){
		e.printStackTrace();
		return null;
	}
}
//-----------------�޸�δ��˵���ٵ�
public boolean updateApply(int status,String reason, String remark){
	int flag=0;
	String sql="update Apply set reason='"+reason+"',remark='"+remark+"' where status=1";
	try {
		pstmt=con.prepareStatement(sql);
		 flag=pstmt.executeUpdate();
		if(flag!=0){
			return true;
		}
		else
			return false;
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
	}
	
	return true;
}

//------------��ȡ���޸ĵ���ٵ�����ϸ��Ϣ
public Apply updateApplylist(String loginname){
	
    String sql = "select * from Apply where num='"+loginname+"' and status=1";
		
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			
			if(rs.next()){
				Apply apply=new Apply();
				apply.setLoginname(rs.getString("num"));
				apply.setBegintime(rs.getString("begintime"));
				apply.setEndtime(rs.getString("endtime"));
				apply.setDays(rs.getInt("days"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				request.setAttribute("apply", apply);
				return apply;
				
			}
			else
				return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	
		
	}
//-------------��ȡ������ٵ���ٵ�
public Apply cancle (String loginname){
    String sql = "select * from Apply where num='"+loginname+"' and status=2 order by aid DESC";
		
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			
			if(rs.next()){
				Apply apply=new Apply();
				apply.setAid(rs.getInt("aid"));
				apply.setLoginname(rs.getString("num"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				request.setAttribute("apply", apply);
				return apply;
				
			}
			else
				return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	
		
	}
//------------------���
public boolean cancleApply(int numid){
	System.out.println("cancleApply�е�numid"+numid);
	int flag=0;
	String sql="update Apply set status=4 where aid='"+numid+"'";
	try {
		pstmt=con.prepareStatement(sql);
		 flag=pstmt.executeUpdate();
		if(flag!=0){
			return true;
		}
		else
			return false;
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
	}
	
	return true;
}
}
